package com.ziq.zcamp.bean.post

import com.donkingliang.groupedadapter.holder.BaseViewHolder
import com.facebook.drawee.generic.RoundingParams
import com.ziq.base.extension.click
import com.ziq.base.extension.dp
import com.ziq.zcamp.bean.place.DetailGroup
import com.ziq.zcamp.community.SubjectAdapter
import com.ziq.zcamp.databinding.ItemHomeSubjectBinding
import com.ziq.zcamp.databinding.ItemHomeSubjectMoreBinding
import com.ziq.zcamp.util.ImageUtil

class SubjectGroup(val adapter: SubjectAdapter) : DetailGroup() {

    override fun getHeaderViewType(groupPosition: Int): Int {
        return 0
    }

    override fun getChildViewType(groupPosition: Int, childPosition: Int): Int {
        return if(list?.size?:0 >= showMoreLimit){
            if(list?.size?:0 == childPosition){
                SubjectAdapter.TYPE_MORE
            } else {
                SubjectAdapter.TYPE_SUBJECT
            }
        } else {
            SubjectAdapter.TYPE_SUBJECT
        }
    }

    private var list : List<PostSubject>? = null
    val showMoreLimit = 3
    fun setData(datalist: List<PostSubject>) {
        list = if(datalist.size > showMoreLimit){
            datalist.subList(0, showMoreLimit)
        } else {
            datalist
        }
    }

    override fun getChildrenCount(): Int {
        var result = list?.size?:0
        if(result >= showMoreLimit){
            result++
        }
        return result
    }

    override fun hasHeader(groupPosition: Int): Boolean {
        return false
    }

    override fun hasFooter(groupPosition: Int): Boolean {
        return false
    }

    override fun onBindHeaderViewHolder(
        holder: BaseViewHolder?,
        groupPosition: Int
    ) {
    }

    override fun onBindFooterViewHolder(
        holder: BaseViewHolder?,
        groupPosition: Int
    ) {
    }

    override fun onBindChildViewHolder(
        holder: BaseViewHolder?,
        groupPosition: Int,
        childPosition: Int
    ) {
        val viewType = getChildViewType(groupPosition, childPosition)
        when (viewType) {
            SubjectAdapter.TYPE_SUBJECT -> {
                onBindSubject(holder, groupPosition, childPosition)
            }
            SubjectAdapter.TYPE_MORE -> {
                onBindSubjectMore(holder, groupPosition, childPosition)
            }
        }
    }

    private fun onBindSubject(
        holder: BaseViewHolder?,
        groupPosition: Int,
        childPosition: Int
    ){
        holder?.let {
            val binding = ItemHomeSubjectBinding.bind(it.itemView)
            if(list != null){
                val item = list?.get(childPosition)
                if(item != null){
                    ImageUtil.updateImageFromNetwork(binding.image, item.images?.get(0)?.url?:"")
                }
                binding.image.hierarchy.roundingParams = RoundingParams.fromCornersRadius(5.dp.toFloat())
                binding.root.click {
                    if(item != null){
                        adapter.subjectAdapterAction?.itemClick(item)
                    }
                }
            }
        }
    }

    private fun onBindSubjectMore(
        holder: BaseViewHolder?,
        groupPosition: Int,
        childPosition: Int
    ){
        holder?.let {
            val binding = ItemHomeSubjectMoreBinding.bind(it.itemView)
            binding.root.click {
                adapter.subjectAdapterAction?.moreClick()
            }
        }
    }


}